package com.rst.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.rst.model.Book;

@Repository
@Transactional
public class BookDAO {

	@Autowired
	private SessionFactory sessionFactory;

	public void saveBook(Book book) {
		sessionFactory.getCurrentSession().saveOrUpdate(book);		
	}

	public Book getBookById(Long bookId) {
	    Session session = sessionFactory.getCurrentSession();
	    Book book = (Book)session.get(Book.class, bookId);
		return book;
	}

	@SuppressWarnings("unchecked")
	public List<Book> getBooksList() {
		Session session = sessionFactory.getCurrentSession();
	    String str = "FROM Book b WHERE b.status = :status  ";
	    	
	    Query query = session.createQuery(str);
	    query.setParameter("status",  Book.ACTIVE);
		return query.list();
	}

	public Book getBookByBookNumber(Long bookNumber) {
		Session session = sessionFactory.getCurrentSession();
	    String str = "FROM Book b WHERE b.bookNumber = :bookNumber AND  b.status = :status  ";
	    	
	    Query query = session.createQuery(str);
	    query.setParameter("status",  Book.ACTIVE);
	    query.setParameter("bookNumber",  bookNumber);
		return (Book) query.uniqueResult();		
	}

	public Object updateBook(Book book) {
		// TODO Auto-generated method stub
		return null;
	}
}




